# -*- coding: UTF-8 -*-
import Jetson.GPIO as GPIO
import time
import Adafruit_SSD1306
from PIL import Image, ImageDraw, ImageFont
import numpy as np

# 设置GPIO模式为BOARD模式
GPIO.setmode(GPIO.BOARD)

# 指定使用的GPIO引脚
button_pin = 7

# 设置GPIO引脚为输入模式，上拉电阻使其默认高电平
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# 去抖动处理
debounce_time = 0.3
last_button_state = GPIO.input(button_pin)
last_change_time = time.time()

# 初始化OLED屏幕
disp = Adafruit_SSD1306.SSD1306_128_64(rst=None, i2c_bus=1, gpio=GPIO)
disp.begin()

# 清空屏幕
disp.clear()
disp.display()

# 创建图像
width, height = disp.width, disp.height
image = Image.new('1', (width, height))
draw = ImageDraw.Draw(image)
font_size = 20  # 根据实际需求调整字体大小

# 使用PIL库加载自定义字体
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
font = ImageFont.truetype(font_path, font_size)

# 显示屏信息
display_width_mm = 22.384
display_height_mm = 5.584
pixel_spacing_mm = 0.175
pixel_size_mm = 0.159

# 计算每个字的像素宽度和高度
pixel_width = int(pixel_size_mm / pixel_spacing_mm)
pixel_height = int(pixel_size_mm / pixel_spacing_mm)

# 定义每个词语的字模
words = {
    "可回收": np.array([
        [0, 0, 1, 1, 1, 0, 0],  
        [0, 1, 0, 0, 0, 1, 0],
        [1, 0, 0, 0, 0, 0, 1],
        [0, 1, 0, 0, 0, 1, 0],
        [0, 0, 1, 1, 1, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
    ]),
    "不可回收": np.array([
        [1, 1, 1, 1, 1, 1, 1],
        [1, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 1],
        [1, 1, 1, 1, 1, 1, 1],
        [1, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 1],
        [1, 1, 1, 1, 1, 1, 1],
    ]),
    "有害": np.array([
        [0, 0, 1, 1, 1, 0, 0],
        [0, 1, 0, 0, 0, 1, 0],
        [1, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 1],
        [0, 1, 0, 0, 0, 1, 0],
        [0, 0, 1, 1, 1, 0, 0],
    ]),
    "厨余": np.array([
        [0, 1, 1, 1, 1, 1, 0],
        [1, 0, 0, 0, 0, 0, 1],
        [0, 1, 0, 0, 0, 1, 0],
        [0, 0, 1, 0, 1, 0, 0],
        [0, 0, 0, 1, 0, 0, 0],
        [0, 1, 0, 0, 0, 1, 0],
        [0, 0, 1, 1, 1, 0, 0],
    ]),
}


# 刷新屏幕显示
disp.image(image)
disp.display()

try:
    print("开始测试")
    while True:
        button_state = GPIO.input(button_pin)
        
        if button_state != last_button_state and time.time() - last_change_time > debounce_time:
            if button_state == GPIO.LOW:
                # 获取当前垃圾类别
                current_word = "可回收"  # 这里根据实际情况修改
                print(f"显示词语：{current_word}")
                
                # 清空屏幕
                draw.rectangle((0, 0, width, height), outline=0, fill=0)

                # 绘制“可回收”的点阵数据到屏幕上
                matrix = words[current_word]
                for i in range(len(matrix)):
                    for j in range(len(matrix[i])):
                        if matrix[i][j] == 1:
                            draw.point((j, i), fill=255)  # 绘制点亮的像素点
            
                # 刷新屏幕显示
                disp.image(image)
                disp.display()
            
            last_button_state = button_state
            last_change_time = time.time()

        time.sleep(0.1)

except KeyboardInterrupt:
    pass

finally:
    GPIO.cleanup()
    disp.clear()
    disp.display()
    print("测试结束")
